package com.runing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runing.entity.RSemester;
import com.runing.entity.RWeek;
import com.runing.mapper.RSemesterMapper;
import com.runing.service.RSemesterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2021-03-11
 */
@Service
public class RSemesterServiceImpl extends ServiceImpl<RSemesterMapper, RSemester> implements RSemesterService {

    @Autowired
    RSemesterMapper rSemesterMapper;

    @Scheduled(cron = "0 0 0 28 8,2 ?")
    public void addSemester(){
        Calendar cal = Calendar.getInstance();
        int month = (cal.get(Calendar.MONTH)) + 1;
        int year = cal.get(Calendar.YEAR);
        RSemester rSemester = new RSemester();
        if (month< 7){
            rSemester.setSename(year+"年上半学期");
        }else {
            rSemester.setSename(year+"年下半学期");
        }
        rSemesterMapper.insert(rSemester);
    }

    public RSemester findNowSemester(){
        QueryWrapper<RSemester> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("seid").last("limit 1");
        RSemester semester = rSemesterMapper.selectOne(queryWrapper);
        return semester;
    }

    public List<RSemester> findAllRSemester(){
        List<RSemester> semesterList = rSemesterMapper.selectList(null);
        return semesterList;
    }

}
